package com.woniu.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;

/**
 * 判断当前日期是否为工作日
 */
public class IsWorkingDayUtil {

    /**
     *
     * @param time 传入的今日日期 格式 20200501
     * @return 返回值：y 是工作日， n 不是
     */
    public static String getIsWorkingDay(String time){
        String dateFlag = getHoliday(time);
        if ("0".equals(dateFlag)) {
            return "y";
        } else {
            return "n";
        }
    }


    /**
     *
     * 访问接口，根据返回值判断当前日期是否为工作日，
     * 返回结果：工作日 0, 休息日 1, 节假日 2；
     */
    public static  String  getHoliday(String  time) {
        String dc = "http://tool.bitefu.net/jiari/?d=";
        String  httpUrl=dc+time;
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {

        }
        return result;
    }
}
